#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    int minimumDifference(vector<int> nums, int k) {
        sort(nums.begin(),nums.end());
        int minDiff = nums[nums.size()-1] - nums[0];
        for (size_t i=k-1; i<nums.size();i++) {
            int dif = nums[i] - nums[i-k+1];
            minDiff = min(minDiff, dif);
        }
        return minDiff;
    }
};

int main() {
    cout << Solution().minimumDifference({9,4,1,7}, 2) <<endl;
    return 0;
}
